package com.lanou.bookstore.category.web.servlet.admin;

import com.lanou.bookstore.book.dao.BookDao;
import com.lanou.bookstore.book.dao.BookDaoImpl;
import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.category.service.CategoryService;
import com.lanou.bookstore.category.service.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/accs")
public class AdminChangeCategoryServlet extends HttpServlet {

    CategoryService cs = new CategoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String cname = req.getParameter("cname");
        String para = req.getParameter("para");
        String input = req.getParameter("input");

        if ("del".equals(para)){

            try {
                cs.delete(cname);
            } catch (Exception e) {
                req.setAttribute("msg", e.toString());
                req.getRequestDispatcher("/adminjsps/admin/msg.jsp").forward(req, resp);
            }

        }else if ("mod".equals(para)){
            cs.mod(cname,input);
        }else if ("add".equals(para)) {
            try {
                cs.add(cname);
            } catch (Exception e) {


                String message = e.toString();
                message=message.replaceAll("[A-Za-z]|[.]|[;]|[:]","");
                req.setAttribute("msg", message);
                req.getRequestDispatcher("/adminjsps/admin/msg.jsp").forward(req, resp);
            }
        }

        req.getRequestDispatcher("/acs?para=query").forward(req, resp);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        doGet(req, resp);
    }
}
